package com.wnxy.controller;

import com.wnxy.domain.Product;
import com.wnxy.service.IProductService;
import com.wnxy.util.ResultConstant;
import com.wnxy.util.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @Classname ProductController
 * @Description :
 * @Date 2021/2/23 18:05
 * @Created by Yeheng
 */
@RestController()
public class ProductController {

    @Autowired
    IProductService service;

    @GetMapping("/product/findOne/{pid}")
    public ResultVO findOne(@PathVariable Integer pid){
        Product one = service.findOne(pid);
        ResultVO.success(one);
        return ResultVO.success(one);
    }

    @GetMapping("/product/findAll")
    public ResultVO findAll(HttpServletRequest request){
        System.out.println(11111111);
        ResultVO success = ResultVO.success(service.findAll());
        success.setMessage(request.getRequestURL().toString());
        return success;
    }

    @GetMapping("/product/reduceStock/{pid}/{num}")
    public ResultVO reduceStock(@PathVariable Integer pid,@PathVariable Integer num){
        try{
            service.reduceStock(pid,num);
            return ResultVO.success(null);
        } catch(Exception e){
            e.printStackTrace();
            return new ResultVO(ResultConstant.OP_FAIL,e.getMessage());
        }
    }

    @GetMapping("/order/get2")
    public String get1(HttpServletRequest request){
        System.out.println(111111);
        return request.getRequestURL().toString();
    }
}
